package com.googlecode.gwtbb.client.only.widget.form;

import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PopupPanel;
import com.googlecode.gwtbb.client.shared.util.StringUtils;

/**
 * @author Abraham Grief
 * @version $Id$
 * @since Jul 21, 2008
 */
public class FormSuccessPopupPanel extends PopupPanel implements CompleteProcessListener {
	private FormWidget formWidget;
	private String processSuccess;

	public FormSuccessPopupPanel() {
		super(true);
		setStylePrimaryName("gwtbb-successMessage");
	}

	public FormSuccessPopupPanel(FormWidget formWidget) {
		this();
		setFormWidget(formWidget);
	}

	public FormWidget getFormWidget() {
		return formWidget;
	}

	public void onProcessCompletion(ProcessEvent processEvent) {
		// show the success message if there is one
		if (StringUtils.isNotEmpty(processSuccess))
			center();
	}

	public void setFormWidget(FormWidget formWidget) {
		if (this.formWidget != null) {
			this.formWidget.getCompleteProcessListenerCollection().removeCompleteProcessListener(this);
		}
		this.formWidget = formWidget;
		formWidget.getCompleteProcessListenerCollection().addCompleteProcessListener(this);
		processSuccess = formWidget.getProcessSuccess();
		setWidget(StringUtils.isNotEmpty(processSuccess) ? new Label(processSuccess) : null);
	}
}
